package com.atguigu.eduservice.service.impl;

import com.atguigu.config.exceptionhandler.GuliException;
import com.atguigu.eduservice.entity.EduCourse;
import com.atguigu.eduservice.entity.EduCourseDescription;
import com.atguigu.eduservice.entity.EduTeacher;
import com.atguigu.eduservice.entity.frontVo.CourseFrontVo;
import com.atguigu.eduservice.entity.frontVo.CourseWebVo;
import com.atguigu.eduservice.entity.vo.CourseInfoVo;
import com.atguigu.eduservice.entity.vo.CoursePublishVo;
import com.atguigu.eduservice.entity.vo.CourseQuery;
import com.atguigu.eduservice.entity.vo.CourseTable;
import com.atguigu.eduservice.mapper.EduCourseMapper;
import com.atguigu.eduservice.service.*;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 课程 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-06
 */
@Service
public class EduCourseServiceImpl extends ServiceImpl<EduCourseMapper, EduCourse> implements EduCourseService {

    @Autowired
    private EduCourseDescriptionService courseDescriptionService;

    @Autowired
    private EduVideoService videoService;

    @Autowired
    private EduChapterService chapterService;

    @Autowired
    private EduTeacherService teacherService;

    @Resource
    private EduCourseMapper eduCourseMapper;

    @Override
    public String saveCourseInfo(CourseInfoVo courseInfoVo) {

        //1、向课程表中添加课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo, eduCourse);
        int insert = baseMapper.insert(eduCourse);
        if(insert <= 0) {
            throw new GuliException(20001,"添加课程失败");
        }
        String cid = eduCourse.getId();

        //2、向课程简介表中添加课程简介
        EduCourseDescription courseDescription = new EduCourseDescription();
        courseDescription.setDescription(courseInfoVo.getDescription());
        courseDescription.setId(cid);
        courseDescription.setId(eduCourse.getId());

        courseDescriptionService.save(courseDescription);

        return cid;

    }

    @Override
    public CourseInfoVo getCourseInfo(String courseId) {

        //查询课程基本信息
        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseInfoVo courseInfoVo = new CourseInfoVo();
        BeanUtils.copyProperties(eduCourse,courseInfoVo);

        //查询描述表
        EduCourseDescription courseDescription = courseDescriptionService.getById(courseId);
        if(courseDescription != null) {

            courseInfoVo.setDescription(courseDescription.getDescription());
        }

        return courseInfoVo;
    }

    @Override
    public void updateCourseInfo(CourseInfoVo courseInfoVo) {
        //修改课程基本信息
        EduCourse eduCourse = new EduCourse();
        BeanUtils.copyProperties(courseInfoVo,eduCourse);
        int update = baseMapper.updateById(eduCourse);
        if(update == 0) {
            throw new GuliException(20001,"修改课程信息失败");
        }
        //修改课程描述信息
        EduCourseDescription description = new EduCourseDescription();
        description.setId(courseInfoVo.getId());
        description.setDescription(courseInfoVo.getDescription());
        courseDescriptionService.updateById(description);

    }

    @Override
    public CoursePublishVo publishCourse(String courseId) {
        //调用Mapper
        CoursePublishVo publishCourseInfo = baseMapper.getPublishCourseInfo(courseId);
        return publishCourseInfo;
    }


    @Override
    public List<CourseTable> getPage(long current, long limit, CourseQuery courseQuery) {


        //判断条件值是否为空,如果不为空拼接条件
        String courseTitle = courseQuery.getCourseTitle();
        String teacherName = courseQuery.getTeacherName();
        String subjectId = courseQuery.getSubjectId();
        String subjectParentId = courseQuery.getSubjectParentId();
        long start = (current-1) * limit;
        List<CourseTable> courseTable = eduCourseMapper.getCourseTable(start, limit, courseTitle, teacherName, subjectId, subjectParentId);
        return courseTable;
    }

    @Override
    public void removeCourse(String courseId) {
        //1、根据课程Id删除课程小节
        videoService.removeVideoByCourseId(courseId);
        //2、根据课程Id删除课程章节
        chapterService.removeChapterByCourseId(courseId);
        //3、根据课程Id删除课程描述,课程描述id和课程id一对一
        courseDescriptionService.removeById(courseId);
        //4、根据课程Id删除课程本身
        int result = baseMapper.deleteById(courseId);
        if(result == 0) {
            throw new GuliException(20001,"删除课程失败");
        }
    }

    @Override
    public int getTotal(CourseQuery courseQuery) {
        int total = eduCourseMapper.getCourseTableTotal(courseQuery);
        return total;
    }

    @Override
    public Map<String, Object> getCourseList(long page, long limit, CourseFrontVo courseFrontVo) {
        Page<EduCourse> pageCourse = new Page<>(page,limit);
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(courseFrontVo.getSubjectParentId())) {
            wrapper.eq("subject_parent_id",courseFrontVo.getSubjectParentId());
        }

        if(!StringUtils.isEmpty(courseFrontVo.getSubjectId())) {
            wrapper.eq("subject_id",courseFrontVo.getSubjectId());
        }

        if(!StringUtils.isEmpty(courseFrontVo.getBuyCountSort())) {
            wrapper.orderByDesc("buy_count");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getGmtCreateSort())) {
            wrapper.orderByDesc("gmt_create");
        }
        if(!StringUtils.isEmpty(courseFrontVo.getPriceSort())) {
            wrapper.orderByDesc("price");
        }

        baseMapper.selectPage(pageCourse,wrapper);

        List<EduCourse> records = pageCourse.getRecords();  //每页记录数据集合
        long current = pageCourse.getCurrent(); //当前页
        long pages = pageCourse.getPages();  //总页数
        long total = pageCourse.getTotal();  //总记录数
        long size = pageCourse.getSize();    //每页记录数

        boolean hasNext = pageCourse.hasNext();  //是否有下一页
        boolean hasPrevious = pageCourse.hasPrevious();   //是否有上一页

        //把分页数据取出，存到集合中
        Map<String, Object> map = new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

    @Override
    public CourseWebVo getBaseCourseInfo(String courseId) {

        EduCourse eduCourse = baseMapper.selectById(courseId);
        CourseWebVo courseWebVo = new CourseWebVo();

        String teacherId = eduCourse.getTeacherId();
        EduTeacher eduTeacher = teacherService.getById(teacherId);

        courseWebVo.setId(eduCourse.getId());
        courseWebVo.setTitle(eduCourse.getTitle());
        courseWebVo.setPrice(eduCourse.getPrice());
        courseWebVo.setLessonNum(eduCourse.getLessonNum());
        courseWebVo.setCover(eduCourse.getCover());
        courseWebVo.setTeacherId(teacherId);
        courseWebVo.setTeacherName(eduTeacher.getName());
        //System.out.println(courseWebVo);
        return courseWebVo;
    }

}
